package ch.avens.roo.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ch.avens.roo.domain.Account;
import ch.avens.roo.domain.Quota;
import flexjson.JSONSerializer;

@RooWebScaffold(path = "accounts", formBackingObject = Account.class)
@RequestMapping("/accounts")
@Controller
@RooJson
public class AccountController {

    @RequestMapping(value = "/quotas", method = RequestMethod.GET, headers = "Accept=application/json")
    @ResponseBody
    public String quotaUsedJson() {
        final List<Quota> quotas = new ArrayList<Quota>();
        for (final Account account : Account.findAllAccounts()) {
            quotas.add(new Quota(account, 512, 1024));
        }

        return new JSONSerializer().exclude("*.class").serialize(quotas);
    }

    @RequestMapping(value = "/quotas/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
    @ResponseBody
    public String quotaUsedJson(@PathVariable("id") final Long id) {
        return new JSONSerializer().exclude("*.class").serialize(new Quota(Account.findAccount(id), 512, 1024));
    }
}
